package com.yksj.healthtalk.ui.doctorstation;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.adapter.DoctorStationGroupListAdapter;
import com.yksj.healthtalk.comm.BuyNumFramentDialog;
import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.chatting.ChatActivity;
import com.yksj.healthtalk.ui.salon.SalonSelectPaymentOptionActivity.OnBuyTicketHandlerListener;
import com.yksj.healthtalk.utils.SalonHttpUtil;
import com.yksj.healthtalk.utils.ToastUtil;


/**
 * 
 * 群聊列表
 * @author zhao
 * 
 */
public class GroupListFragment extends Fragment implements OnClickListener,OnBuyTicketHandlerListener{
	DoctorStationGroupListAdapter mAdapter;
	ListView mListView;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.listview_layout,null);
		mListView = (ListView)view.findViewById(R.id.listview);
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				GroupInfoEntity entity = mAdapter.getItem(position);
				onSendJoinGoupChat(entity);
			}
		});
		AppData appData = HTalkApplication.getAppData();
		mAdapter = new DoctorStationGroupListAdapter(appData.getInterestGroupInfoList(),inflater,getActivity());
		mListView.setAdapter(mAdapter);
		initView(view);
		return view;
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			getActivity().onBackPressed();
			break;
		}
	}
	
	/**
	 * 加入群聊
	 * @param entity
	 */
	private void onSendJoinGoupChat(GroupInfoEntity entity){
		SalonHttpUtil.onItemClick(getActivity(),this,getChildFragmentManager(),entity,false);
	}
	
	private void initView(View view){
		view.findViewById(R.id.title_back).setOnClickListener(this);
		TextView textView = (TextView)view.findViewById(R.id.title_lable);
		textView.setText("话题");
		
	}
	
	/**
	 * 
	 * 门票请求加入聊天
	 * @author zhao
	 */
	class GroupChatHttpResponseHandler extends JsonHttpResponseHandler{
		final GroupInfoEntity mGroupInfoEntity;
		public GroupChatHttpResponseHandler(GroupInfoEntity entity) {
			this.mGroupInfoEntity = entity;
		}
		@Override
		public void onSuccess(int statusCode, JSONObject response) {
			super.onSuccess(statusCode, response);
			try {
				String code = response.getString("CODE");
				if("-1".equals(code)){//收取门票
					int dayMoney = 0;//日票
					int monthMoney = 0;//月票
					JSONArray array  = response.getJSONArray("TICKET");
					for (int i = 0; i < array.length(); i++) {
						JSONObject jsonObject = array.getJSONObject(i);
						int charge =jsonObject.getInt("CHARGING_STANDARDS");
//						int defineType =jsonObject.getInt("DEFINE_TYPE");
						int ticketType =jsonObject.getInt("TICKET_TYPE");
						if(ticketType == 1){//日票
							dayMoney  = charge;
						}else if(ticketType == 2){//月票
							monthMoney  = charge;
						}
					}
					BuyNumFramentDialog.showLodingDialog(
							getChildFragmentManager(),
							mGroupInfoEntity,
							mGroupInfoEntity.getName(),
							dayMoney,
							monthMoney
							);
				}else{//不收门票
					Intent  intent1 = new Intent();
					intent1.putExtra(ChatActivity.KEY_PARAME,mGroupInfoEntity);
					intent1.setClass(getActivity(),ChatActivity.class);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		@Override
		public void onFinish() {
			super.onFinish();
			LodingFragmentDialog.dismiss(getChildFragmentManager());
		}
	}

	/**
	 * 购买门票返回
	 */
	@Override
	public void onTicketHandler(String state, GroupInfoEntity entity) {
		if("1".equals(state)){
			Intent  intent1 = new Intent(getActivity(),ChatActivity.class);
			intent1.putExtra(ChatActivity.KEY_PARAME,entity);
			startActivity(intent1);
		}else if("0".equals(state)){
			ToastUtil.showLong(getActivity(),"多美币不足");
		}else if("-1".equals(state)){
			ToastUtil.showLong(getActivity(),"支付失败");
		}
	}
	
}
